<?php
// .-----------------------------------------------------------------------------------
// | WE TRY THE BEST WAY 杭州博也网络科技有限公司
// |-----------------------------------------------------------------------------------
// | Author: 贝贝 <hebiduhebi@163.com>
// | Copyright (c) 2013-2016, http://www.itboye.com. All Rights Reserved.
// |-----------------------------------------------------------------------------------
namespace Admin\Controller;

use Admin\Api\LangApi;
use Shop\Api\CategoryPropApi;
use Shop\Api\CategoryPropvalueApi;
use Think\Exception;

class CategoryPropvalueController extends AdminController{
	
	private $cate_id;
	private $prop_key;
	
	protected function _initialize(){
		parent::_initialize();
		$this->cate_id = I('cate_id',-1);
		$this->assign("cate_id",$this->cate_id);
		$this->prop_key = $this->_param('prop_key',-1);
		$this->assign("prop_key",$this->prop_key);

	}
	
	public function index(){
//		$prop_id = I('prop_id',-1);
		$lang = I('lang');
		$map = array(
			'prop_keyid'=>$this->prop_key,
            'lang'=>'zh-cn',
		);
		$params = array(
            'prop_key'=>$this->prop_key
		);

		$result = (new CategoryPropApi())->getInfo(['propid'=>$this->prop_key,'lang'=>'zh-cn']);
		$this->assign('prop',$result['info']);
				
		$page = array('curpage' => I('get.p', 0), 'size' => C('LIST_ROWS'));
		
		$order = " id asc ";
		//
		$result = apiCall(CategoryPropvalueApi::QUERY,array($map,$page,$order,$params));
		
		//
		if($result['status']){
//			$this->assign('prop_id',$prop_id);
			$this->assign('lang',$lang);
			$this->assign('show',$result['info']['show']);
			$this->assign('list',$result['info']['list']);
			$this->display();
		}else{
			LogRecord('INFO:'.$result['info'],'[FILE] '.__FILE__.' [LINE] '.__LINE__);
			$this->error(L('UNKNOWN_ERR'));
		}
	}

	public function add(){
		$prop_id = I('prop_id',-1);
		if(IS_GET){

            $this->assign("lang_list",LangApi::getLangSupport());
            $this->assign('prop_id',$prop_id);
            $this->assign('prop_key',$this->prop_key);
			$this->display();
		}else{

            $langs = I('post.value_lang',[]);
            $langs_value = [];
            $default_lang_value = "";
            foreach ($langs as $vo){
                $cate_lang_value = I('post.value_lang_'.md5($vo),'');
                if($vo == 'zh-cn'){
                    $default_lang_value = $cate_lang_value;
                }
                if(empty($cate_lang_value)){
                    $cate_lang_value = $default_lang_value;
                }
                array_push($langs_value,$cate_lang_value);
            }



            $result = (new CategoryPropApi())->queryNoPaging(['propid'=>$this->prop_key]);
            $list = $result['info'];

            $allEntity = [];
            $value_key = md5(time().$prop_id);
            for($i=0;$i< count($langs);$i++){
                foreach ($list as $vo){
                    if($vo['lang'] == $langs[$i]){
                        $prop_id = $vo['id'];
                        array_push($allEntity,[
                            'prop_keyid'=>$this->prop_key,
                            'prop_id' => $prop_id,
                            'valuename'=>$langs_value[$i],
                            'lang'=>$langs[$i],
                            'value_key'=>$value_key,
                        ]);
                        break;
                    }
                }
            }

            if(count($allEntity) == 0){
                $this->error("操作失败");
            }


            $model = M();
            $model->startTrans();
            $result = (new CategoryPropvalueApi())->addAll($allEntity);

			if($result['status']){
                $model->commit();
				$this->success("添加成功！",U('Admin/CategoryPropvalue/index',array('prop_key'=>$this->prop_key,'prop_id'=>$prop_id,'cate_id'=>$this->cate_id)));
			}
			else{
                $model->rollback();
				$this->error($result['info']);
			}
			
		}
	}
	
	public function edit(){
        $prop_id = I('prop_id',-1);
		$id = I('id','');
        $value_key = $this->_param('value_key','-1');
		if(IS_GET){
			$result = (new CategoryPropvalueApi())->queryNoPaging(['value_key'=>$value_key]);
            $list   = $result['info'];
            $lang_list = LangApi::getLangSupport();
            foreach ($list as $vo){
                foreach ($lang_list as &$lang) {

                    if ($vo['lang'] == $lang['value']) {
                        $lang['value_id'] = $vo['id'];
                        $lang['value_name'] = $vo['valuename'];
                        break;
                    }
                }
            }
			$this->assign('value_key',$value_key);
            $this->assign("lang_list",$lang_list);
			$this->assign('prop_id',$prop_id);
			$this->display();
		}else{

            $ids = I('post.ids',[]);
            $langs = I('post.value_lang',[]);
            $langs_value = [];
            $default_lang_value = "";
            foreach ($langs as $vo){
                $cate_lang_value = I('post.value_lang_'.md5($vo),'');
                if($vo == 'zh-cn'){
                    $default_lang_value = $cate_lang_value;
                }
                if(empty($cate_lang_value)){
                    $cate_lang_value = $default_lang_value;
                }
                array_push($langs_value,$cate_lang_value);
            }



            $result = (new CategoryPropApi())->queryNoPaging(['propid'=>$this->prop_key]);
            $list = $result['info'];

            $allEntity = [];
            for($i=0;$i< count($langs);$i++){
                foreach ($list as $vo){
                    if($vo['lang'] == $langs[$i]){
                        $prop_id = $vo['id'];
                        array_push($allEntity,[
                            'prop_keyid'=>$this->prop_key,
                            'prop_id' => $prop_id,
                            'valuename'=>$langs_value[$i],
                            'lang'=>$langs[$i],
                            'value_key'=>$value_key
                        ]);
                        break;
                    }
                }
            }

            if(count($allEntity) == 0){
                $this->error("操作失败");
            }


            $model = M();
            try{
                $model->startTrans();
                for($i=0;$i<count($ids);$i++){
                    $result = (new CategoryPropvalueApi())->saveByID($ids[$i],$allEntity[$i]);
                }

                if($result['status']){
                    $model->commit();
                    $this->success("保存成功！",U('Admin/CategoryPropvalue/index',array('prop_key'=>$this->prop_key,'prop_id'=>$prop_id,'cate_id'=>$this->cate_id)));
                }
                else{
                    $model->rollback();
                    $this->error($result['info']);
                }
            }catch (Exception $exception){
                $model->rollback();
                $this->error($exception->getMessage());
            }
		}
	}

	
	public function delete(){
        $value_key = I('get.value_key','-1');
        $result = (new CategoryPropvalueApi())->delete(['value_key'=>$value_key]);
		if($result['status']){
			$this->success("删除成功！");
		}else{
			$this->error($result['info']);
		}
		
	}

	
	public function bulkDelete(){
	    $ids = $this->_param('ids',[]);
	    foreach ($ids as $value_key){
            $result = (new CategoryPropvalueApi())->delete(['value_key'=>$value_key]);

            if(!$result['status']){
                $this->error($result['info']);
            }
	    }

        $this->success("删除成功！");
    }
	
	
}

